﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//-------------------------------------------------------------------------------------

using System;
using System.Windows.Forms;

namespace DotNet.WinForm.LogOn
{
    using DevExpress.Skins;
    using DotNet.Utilities;
    using DotNet.WinForm.Utilities;

    /// <summary>
    /// FrmLogOn
    /// 用户登录系统
    ///
    /// 修改纪录
    ///
    ///		2010.10.17 版本：2.1 DotNet 多次输入错误密码后，要求有提示信息再退出程序。
    ///		2010.09.19 版本：2.0 DotNet 完善记住密码功能，密码按加密方式保存。
    ///		2010.02.26 版本：1.6 DotNet 只有内部用户才能登录。
    ///		2009.01.19 版本：1.5 DotNet SAP登录问题解决。
    ///		2009.01.19 版本：1.4 DotNet 下拉框为用户的真实姓名问题解决。
    ///		2008.03.21 版本：1.3 DotNet 用户名密码不能记录的错误进行改进。
    ///		2007.09.17 版本：1.2 DotNet 窗体上按 ESC 按钮退不出的错误修正。
    ///		2007.08.05 版本：1.1 DotNet 用户名记录起来，提示信息显示，默认允许登录次数。
    ///		2007.06.12 版本：1.0 DotNet 整理文件。
    ///
    /// 版本：2.1
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2010.10.17</date>
    /// </author>
    /// </summary>
    public partial class FrmConfig : BaseForm
    {
        public FrmConfig()
        {
            InitializeComponent();
        }

        #region private void GetConfigInfo() 获取系统配置信息

        /// <summary>
        /// 获取系统配置信息
        /// </summary>
        private void GetConfigInfo()
        {
            this.txtUser.Text = BaseSystemInfo.CurrentUserName;

            // 对密码进行解密操作
            string password = BaseSystemInfo.CurrentPassword;
            password = SecretUtil.Decrypt(password);
            this.txtPassword.Text = password;

            // 初始化语言选项菜单
            this.cmbCurrentLanguage.Items.Clear();
            this.cmbCurrentLanguage.Items.Add("zh-CN");
            this.cmbCurrentLanguage.Items.Add("zh-TW");
            this.cmbCurrentLanguage.Items.Add("en-US");
            this.cmbCurrentLanguage.SelectedIndex = this.cmbCurrentLanguage.Items.IndexOf(BaseSystemInfo.CurrentLanguage);

            this.chkRememberPassword.Checked = BaseSystemInfo.RememberPassword;
            this.chbAutoLogOn.Checked = BaseSystemInfo.AutoLogOn;
            this.chkRecordLog.Checked = BaseSystemInfo.RecordLog;
            this.chkAllowUserRegister.Checked = BaseSystemInfo.AllowUserRegister;
            this.chkCheckOnLine.Checked = BaseSystemInfo.CheckOnLine;
            this.chkEncryptPassword.Checked = BaseSystemInfo.EncryptPassword;
            this.chkAllowNullPassword.Checked = BaseSystemInfo.AllowNullPassword;

            this.txtRootMenuCode.Text = BaseSystemInfo.RootMenuCode;
            this.txtMainForm.Text = BaseSystemInfo.MainForm;
            this.txtLogOnForm.Text = BaseSystemInfo.LogOnForm;

            this.chkUseModulePermission.Checked = BaseSystemInfo.UseModulePermission;
            this.chkUsePermissionScope.Checked = BaseSystemInfo.UsePermissionScope;
            this.chkUseLicensedPermission.Checked = BaseSystemInfo.UseLicensedPermission;
            this.chkUseTableColumnsPermission.Checked = BaseSystemInfo.UseTableColumnsPermission;

            // 初始化数据库
            this.cmbDataBaseType.Items.Clear();
            this.cmbDataBaseType.Items.Add("SqlServer");
            this.cmbDataBaseType.Items.Add("Oracle");
            this.cmbDataBaseType.Items.Add("Access");
            this.cmbDataBaseType.Items.Add("Bd2");
            this.cmbDataBaseType.Items.Add("MySql");
            this.cmbDataBaseType.Items.Add("SQLite");
            this.cmbDataBaseType.SelectedIndex = this.cmbDataBaseType.Items.IndexOf(BaseSystemInfo.DataBaseType.ToString());

            // 初始化皮肤
            foreach (SkinContainer cnt in SkinManager.Default.Skins)
            {
                this.cmbSkinName.Items.Add(cnt.SkinName);
            }
            this.cmbSkinName.EditValue = BaseSystemInfo.AppSkinName;

            this.chkEncryptDbConnection.Checked = BaseSystemInfo.EncryptDbConnection;
            this.txtUserCenterDbConnection.Text = BaseSystemInfo.UserCenterDbConnectionString;
            this.txtBusinessDbConnection.Text = BaseSystemInfo.BusinessDbConnectionString;
        }

        #endregion private void GetConfigInfo() 获取系统配置信息

        #region private void SaveConfigInfo() 保存系统设置

        /// <summary>
        /// 保存系统设置
        /// </summary>
        private void SaveConfigInfo()
        {
            BaseSystemInfo.CurrentUserName = this.txtUser.Text;
            BaseSystemInfo.CurrentPassword = SecretUtil.Encrypt(this.txtPassword.Text);

            if (this.cmbCurrentLanguage.Text.Length != 0)
            {
                BaseSystemInfo.CurrentLanguage = this.cmbCurrentLanguage.SelectedItem.ToString();
            }

            BaseSystemInfo.RememberPassword = this.chkRememberPassword.Checked;
            BaseSystemInfo.AutoLogOn = this.chbAutoLogOn.Checked;
            BaseSystemInfo.RecordLog = this.chkRecordLog.Checked;
            BaseSystemInfo.AllowUserRegister = this.chkAllowUserRegister.Checked;
            BaseSystemInfo.CheckOnLine = this.chkCheckOnLine.Checked;
            BaseSystemInfo.EncryptPassword = this.chkEncryptPassword.Checked;
            BaseSystemInfo.EncryptDbConnection = this.chkEncryptDbConnection.Checked;
            BaseSystemInfo.AllowNullPassword = this.chkAllowNullPassword.Checked;

            BaseSystemInfo.MainForm = txtMainForm.Text;
            BaseSystemInfo.LogOnForm = this.txtLogOnForm.Text;
            BaseSystemInfo.RootMenuCode = txtRootMenuCode.Text;

            BaseSystemInfo.UseModulePermission = this.chkUseModulePermission.Checked;
            BaseSystemInfo.UsePermissionScope = this.chkUsePermissionScope.Checked;
            BaseSystemInfo.UseLicensedPermission = this.chkUseLicensedPermission.Checked;
            BaseSystemInfo.UseTableColumnsPermission = this.chkUseTableColumnsPermission.Checked;
            BaseSystemInfo.AppSkinName = this.cmbSkinName.Text;
            if (this.cmbDataBaseType.Text.Length != 0)
            {
                BaseSystemInfo.DataBaseType = BaseConfiguration.GetDataBaseType(this.cmbDataBaseType.SelectedItem.ToString());
            }

            BaseSystemInfo.UserCenterDbConnectionString = this.txtUserCenterDbConnection.Text;
            BaseSystemInfo.BusinessDbConnectionString = this.txtBusinessDbConnection.Text;

            // 保存用户的信息
            ConfigHelper.SaveConfig();

            // 从当前指定的语言包读取信息
            AppMessage.GetLanguageResource();

            //重置所有打开窗体的语言

            this.ResetLanguage();
        }

        #endregion private void SaveConfigInfo() 保存系统设置

        private void FrmConfig_Load(object sender, EventArgs e)
        {
            // 获取系统配置信息
            this.GetConfigInfo();
        }

        private void btmEncrypt_Click(object sender, EventArgs e)
        {
            //  如果当前处于未加密状态，则对数据库连接字符窜进行加密操作；
            if (!this.chkEncryptDbConnection.Checked)
            {
                this.chkEncryptDbConnection.Checked = true;
                this.txtUserCenterDbConnection.Text = SecretUtil.Encrypt(this.txtUserCenterDbConnection.Text);
                this.txtBusinessDbConnection.Text = SecretUtil.Encrypt(this.txtBusinessDbConnection.Text);
            }
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            // 如果当前处于已加密状态，则对数据库连接字符窜进行解密操作；
            if (this.chkEncryptDbConnection.Checked)
            {
                this.chkEncryptDbConnection.Checked = false;
                this.txtUserCenterDbConnection.Text = SecretUtil.Decrypt(this.txtUserCenterDbConnection.Text);
                this.txtBusinessDbConnection.Text = SecretUtil.Decrypt(this.txtBusinessDbConnection.Text);
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            // 保存系统设置
            this.SaveConfigInfo();
            DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.MSG0011, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 重置所有打开窗体的语言
        /// </summary>
        private void ResetLanguage()
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form is BaseForm)
                {
                    (form as BaseForm).Localization(form);
                }
            }
        }

        private void cmbSkinName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(cmbSkinName.Text);
        }
    }
}